﻿using System.ComponentModel.Composition;
using Microsoft.Practices.Unity;
using TorchView.Composition;
using TorchView.Extensibility;
using TorchView.Game;
using TorchView.Windows;

namespace TorchView.PakViewer
{
    [Export(typeof(IShellPlugin))]
    public sealed class PakViewerPlugin : IShellPlugin
    {
        private IUnityContainer _Container;

        [Import(typeof(ITorchViewShell))]
        public ITorchViewShell Shell { get; set; }

        private void EnsureInitialized()
        {
            if (_Container == null)
            {
                _Container = Shell == null || Shell.GlobalContainer == null ? new UnityContainer() : Shell.GlobalContainer.CreateChildContainer();

                _Container.RegisterInstance<PakModel>(new PakModel(Torchlight.InstallDirectory));
                
                _Container.RegisterType<IPakViewModel, PakViewModel>(new ContainerControlledLifetimeManager());
                _Container.RegisterType<IView<IPakViewModel>, PakView>(new ContainerControlledLifetimeManager());
                //_Container.RegisterType<IGameExporter, XamlGameExporter>(new ContainerControlledLifetimeManager());
            }
        }

        #region IShellPlugin Members
        public string Name
        {
            get { return "Pak Viewer"; }
        }

        public object MainView
        {
            get
            {
                EnsureInitialized();
                return _Container.Resolve<IPakViewModel>().View;
            }
        }
        #endregion
    }
}
